package bjut.zrpc.core.optimization.message;

import bjut.zrpc.core.optimization.enums.ZRpcResponseEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


import java.io.Serializable;

/**
 * @Author: zhuzhiming
 * @Date: 2022/6/24 1:23
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ZRpcResponseMessage<T> implements Serializable {

    private String requestId;

    private Integer statueCode;

    private String message;

    private T data;


    public static <T> ZRpcResponseMessage<T> success(T data, String requestId) {

        ZRpcResponseMessage<T> zRpcResponseMessage = new ZRpcResponseMessage<>();
        zRpcResponseMessage.setRequestId(requestId);
        zRpcResponseMessage.setStatueCode(ZRpcResponseEnum.SUCCESS.getStatueCode());
        zRpcResponseMessage.setMessage(ZRpcResponseEnum.SUCCESS.getMessage());

        if (data != null) {
            zRpcResponseMessage.setData(data);
        }

        return zRpcResponseMessage;
    }

    public static <T> ZRpcResponseMessage<T> fail(ZRpcResponseEnum zRpcResponseEnum, String requestId) {

        ZRpcResponseMessage<T> zRpcResponseMessage = new ZRpcResponseMessage<>();
        zRpcResponseMessage.setStatueCode(zRpcResponseEnum.getStatueCode());
        zRpcResponseMessage.setMessage(zRpcResponseMessage.getMessage());
        zRpcResponseMessage.setRequestId(requestId);
        return zRpcResponseMessage;

    }


}
